package com.controller;


import com.dao.VetDao;
import com.po.Vet;
import com.po.Vet_specialityKey;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


@Controller
public class VetController {
 /*
    添加医生
    */
    @RequestMapping(value = "/doAddVet")
    public String doAdd(Vet vet, Vet_specialityKey vet_specialityKey, Model model)throws Exception{
       try {
           VetDao U = new VetDao();
           U.addVet(vet);
           vet_specialityKey.setVetid(vet.getId());
           U.addVetSpecs(vet_specialityKey);
           model.addAttribute("msg","添加医生成功");
           return "/doAddVetForFind";
       } catch (Exception e){
           model.addAttribute("msg",e.getMessage());
           return "/doAddVetForFind";
       }
    }
/*
    多对多查询医生及其专业
    */
    @RequestMapping(value = "/doSelectVet")
    public String selectvetByVetId(Vet_specialityKey vet_speciality, Model model)throws Exception{
        try {
            VetDao U = new VetDao();
            List<Vet> vets = U.findvetByVnameAndSname(vet_speciality);
/*            for(int i = 0;i < vets.size(); i ++){
                System.out.println(vets.get(i).getName());
                for(int b=0;b < vets.get(i).getSpecs().size(); b++){
                    String specs = "";
                    specs += vets.get(i).getSpecs().get(b).getName()+",";
                System.out.println(specs);
                }
            }*/
            model.addAttribute("cs",vets);

            return "/vetsearch_result.jsp";
        }catch (Exception e){
            model.addAttribute("msg",e.getMessage());
            return "/vetsearch.jsp";
        }
    }
/*
    删除医生
    */
    @RequestMapping(value = "/doDelVet")
    public String doDel(Vet id,Model model)throws Exception {
        VetDao Delete = new VetDao();
        Delete.deleteVet(id);
        Delete.deleteVetSpecs(id);
        model.addAttribute("msg","删除医生成功");
        return "/vetsearch.jsp";
    }

}
